package com.ys.nms.module.business.controller.admin.redData;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class TimestampConverter {

    // 定义常用的日期时间格式
    private static final DateTimeFormatter DEFAULT_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter SHORT_FORMATTER =
            DateTimeFormatter.ofPattern("MM-dd HH:mm");
    private static final DateTimeFormatter TIME_ONLY_FORMATTER =
            DateTimeFormatter.ofPattern("HH:mm:ss");

    /**
     * 时间戳转默认格式字符串 (yyyy-MM-dd HH:mm:ss)
     */
    public static String timestampToDefaultString(Long timestamp) {
        if (timestamp == null) return "";

        LocalDateTime dateTime = LocalDateTime.ofInstant(
                Instant.ofEpochSecond(timestamp),
                ZoneId.systemDefault()
        );
        return dateTime.format(DEFAULT_FORMATTER);
    }

    /**
     * 时间戳转自定义格式字符串
     */
    public static String timestampToString(Long timestamp, String pattern) {
        if (timestamp == null) return "";

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime dateTime = LocalDateTime.ofInstant(
                Instant.ofEpochSecond(timestamp),
                ZoneId.systemDefault()
        );
        return dateTime.format(formatter);
    }

    /**
     * 时间戳转短格式字符串 (MM-dd HH:mm)
     */
    public static String timestampToShortString(Long timestamp) {
        if (timestamp == null) return "";

        LocalDateTime dateTime = LocalDateTime.ofInstant(
                Instant.ofEpochSecond(timestamp),
                ZoneId.systemDefault()
        );
        return dateTime.format(SHORT_FORMATTER);
    }

    /**
     * 时间戳转时间字符串 (HH:mm:ss)
     */
    public static String timestampToTimeString(Long timestamp) {
        if (timestamp == null) return "";

        LocalDateTime dateTime = LocalDateTime.ofInstant(
                Instant.ofEpochSecond(timestamp),
                ZoneId.systemDefault()
        );
        return dateTime.format(TIME_ONLY_FORMATTER);
    }

    /**
     * 时间戳转ISO格式字符串
     */
    public static String timestampToIsoString(Long timestamp) {
        if (timestamp == null) return "";

        return Instant.ofEpochSecond(timestamp)
                .atZone(ZoneId.systemDefault())
                .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    }
}